package cn.pw.test.com.news.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.aries.ui.view.title.TitleBarView;

import java.util.ArrayList;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.FragmentAdapter;
import tech.com.commoncore.base.BaseTitleFragment;

public class NewsShelfFragment extends BaseTitleFragment {

    private TabLayout tabLayout;
    private String[] titles;
    private ViewPager viewPager;
    private FragmentAdapter fragmentAdapter;

    public static NewsShelfFragment newInstance() {
        NewsShelfFragment fragment = new NewsShelfFragment();
        return fragment;
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setLeftTextDrawable(null)
                .setTitleMainText(getResources().getString(R.string.news_app_name));
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_news_shelf;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tabLayout = mContentView.findViewById(R.id.tab_layout);
        viewPager = mContentView.findViewById(R.id.view_pager);

        titles = new String[]{getString(R.string.news_string_hot_title),
                getString(R.string.news_string_newsletter_title),
                getString(R.string.news_string_video_title),
                getString(R.string.news_string_focus_news)};

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(ArticleFragment.newInstance("195", false));
        fragments.add(NewsletterFragment.newInstance());
        fragments.add(VideoFragment.newInstance());
        fragments.add(ArticleFragment.newInstance("192", false));

        fragmentAdapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        viewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(fragmentAdapter);
    }
}
